    /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package view;

import controller.WorkerSimulacion;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import static java.time.Clock.system;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import model.Nodo;
import model.Simulacion;

/**
 *
 * @author jeancarlo Ynga
 */
public class SimulacionMapa2 extends JFrame implements Runnable {
    
    //private BufferedImage imgDibujar, imgFondo;
    //private BufferStrategy bufferStrategy;
    Simulacion simulacion;
    List<Nodo> listaNodos;
    Image imgFondoMapa;
    int xMapa=100,yMapa=100;
    private WorkerSimulacion worker;

    public List<Nodo> getListaNodos() {
        return listaNodos;
    }

    public void setListaNodos(List<Nodo> listaNodos) {
        this.listaNodos = listaNodos;
    }

    public SimulacionMapa2(Simulacion simulacion) throws IOException {
        this.worker = null;
        this.simulacion = simulacion;
        this.listaNodos = simulacion.getListaNodos();
        
        setResizable(false);
        setSize(new java.awt.Dimension(1550, 800));
        setLocationRelativeTo(null);
        //setBounds(10,10,1550,800);
        setTitle("Simulacion");
        
        //Toolkit tk = getToolkit();
        //imgFondoMapa = tk.createImage("Mapa.gif");
        //imgFondoMapa = ImageIO.read(new File("Mapa.bmp"));
        
        
        //imgFondoMapa = ImageIO.read(new File("src/resources/Mapa2.bmp"));
        //lblExit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/salir.png")));
        imgFondoMapa =  ImageIO.read( ClassLoader.getSystemResource( "resources/Mapa2.bmp" ) );
        
        
        
        //createBufferStrategy(2);
        //bufferStrategy = getBufferStrategy();
        
        setVisible(true);
        
    }
    
    public SimulacionMapa2(Simulacion simulacion, WorkerSimulacion worker) throws IOException {
        this.worker = worker;
        this.simulacion = simulacion;
        this.listaNodos = simulacion.getListaNodos();
        
        this.addWindowListener(new WorkerSimulacionListener(this.worker));
        
        setResizable(false);
        setSize(new java.awt.Dimension(1550, 800));
        setLocationRelativeTo(null);
        //setBounds(10,10,1550,800);
        setTitle("Simulacion");
        
        //Toolkit tk = getToolkit();
        //imgFondoMapa = tk.createImage("Mapa.gif");
        //imgFondoMapa = ImageIO.read(new File("Mapa.bmp"));
        
        
        //imgFondoMapa = ImageIO.read(new File("src/resources/Mapa2.bmp"));
        //lblExit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/salir.png")));
        imgFondoMapa =  ImageIO.read( ClassLoader.getSystemResource( "resources/Mapa2.bmp" ) );
        
        
        
        //createBufferStrategy(2);
        //bufferStrategy = getBufferStrategy();
        
        setVisible(true);
        
    }
    
    
    public void paint(Graphics g){
		//super.paint(g);
        int capMax,capDisp,cantPqts,posX,posY; 
        double porcLibre;
        Color  color;
        
        //DIBUJO EL MAPA DE FONDO
        g.drawImage(imgFondoMapa,0,0,this);
        
        //DIBUJO TIEMPO INICIO Y MODIFICAR
        Font fuente=new Font("Monospaced", Font.BOLD, 15);
        g.setFont(fuente);
        DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm");
        //System.out.println(simulacion.getFechaInicioSim());System.out.println(simulacion.getFechaHoraSim());
        String TInicioSim= "Inicio de Simulacion:\t" + df.format(simulacion.getFechaInicioSim());
        g.drawString(TInicioSim,xMapa+1000, yMapa+10);
        String TActualSim= "Tiempo Actual de Simulacion:\t" + df.format(simulacion.getFechaHoraSim());
        g.drawString(TActualSim,xMapa+1000, yMapa+60);
        
        //PARA CADA CIUDAD(NODO)
        for (int i = 0; i < listaNodos.size(); i++) {
            Nodo nodo = listaNodos.get(i);
            capMax= nodo.getCiudad().getCapMax();
            capDisp=nodo.getCiudad().getCapDisp();
            cantPqts=capMax - capDisp;
            posX=nodo.getCiudad().getX();
            posY=nodo.getCiudad().getY();
            
            porcLibre=(double)capDisp/(double)capMax;
            if (porcLibre>=0.5){// mayor a 50%
                color=Color.GREEN;
            }else //menor a 50% 
                if (porcLibre>=0.2){ //mayor a 20%
                    color=Color.YELLOW;
                }else color=Color.RED;
        
            //DIBUJO EL SEMAFORO
            g.setColor(Color.BLACK);
            g.fillOval(xMapa+posX-10, yMapa+posY-10, 20, 20);
            g.setColor(color);
            g.fillOval(xMapa+posX-10, yMapa+posY-10, 18, 18);
//            for (int j = 1; j < 20; j++) {
//                if(j==18)g.setColor(Color.BLACK);
//                g.drawOval(xMapa+posX-10, yMapa+posY-10, j, j);
//                //g.drawArc(xMapa+posX-10, yMapa+posY-10, j, j,0,360);
//            }
            g.setColor(Color.BLUE);
            fuente=new Font("Monospaced", Font.BOLD, 15);
            g.setFont(fuente);
            String cadena=(Integer.toString(cantPqts).concat("/")).concat(Integer.toString(capMax));
            g.drawString(cadena,xMapa+posX-30, yMapa+posY-10);
            
        }
        
        
		
	
        //Graphics2D graph2D = (Graphics2D)bufferStrategy.getDrawGraphics();
        //graph2D.drawImage(imgFondo, xMapa, yMapa, this);
        //graph2D.drawOval(xMapa, yMapa, 100,100);
        //bufferStrategy.show();
        
       
    		
    }

    public void actualizarMapa(Simulacion simulacion) {
        this.simulacion = simulacion;
        this.listaNodos = simulacion.getListaNodos();
        repaint();
    }

    @Override
    public void run() {
        actualizarMapa(this.simulacion);
    }
    
    
    
    
    
    
    
}


class WorkerSimulacionListener implements WindowListener{

    private WorkerSimulacion worker;
    
    public WorkerSimulacionListener(WorkerSimulacion worker){
        this.worker = worker;
    }

    @Override
    public void windowOpened(WindowEvent e) {
        
    }

    @Override
    public void windowClosing(WindowEvent e) {
        
        int dialogButton = JOptionPane.YES_NO_OPTION;
        int dialogResult = JOptionPane.showConfirmDialog (null, "¿Seguro que desea salir de la simulación?","Confirmación",dialogButton);
        if(dialogResult == JOptionPane.YES_OPTION){ // si el usuario eligio "SI"
        
        //System.out.println("listener");
        this.worker.cancel(true); //To change body of generated methods, choose Tools | Templates.
        Thread.currentThread().interrupt();
        new Menu().setVisible(true);
        return;
        //new Menu().setVisible(true);
        }
    }

    @Override
    public void windowClosed(WindowEvent e) {
        
    }

    @Override
    public void windowIconified(WindowEvent e) {
        
    }

    @Override
    public void windowDeiconified(WindowEvent e) {
        
    }

    @Override
    public void windowActivated(WindowEvent e) {
        
    }

    @Override
    public void windowDeactivated(WindowEvent e) {
        
    }
    
}
